﻿using System;
using System.Linq;
using System.Management;
using ClusterAware.Properties;
using ClusterAwareProperties;
using ClusterAwareWMI;

namespace ClusterAware
{
    /// <summary>
    /// Wrapper property to ResourceType Class actions
    /// </summary>
    public class ResourceTypeMethods
    {

        #region private properties

        private readonly ClusterManager _managerclass;

        #endregion

        #region Ctor

        /// <summary>
        /// private Constructor
        /// </summary>
        private ResourceTypeMethods()
        {
        }

        /// <summary>
        /// Create Class ResourceTypeMethods
        /// </summary>
        /// <param name="managerclass">Instance of ClusterManager class</param>
        public ResourceTypeMethods(ClusterManager managerclass)
            : this()
        {
            _managerclass = managerclass;
        }

        #endregion

        #region public Methods

        /// <summary>
        /// CusterManager Instance
        /// </summary>
        public ClusterManager ManagerInstance
        {
            get { return _managerclass; }
        }
        
        /// <summary>
        /// if Exists ResurceType
        /// </summary>
        /// <param name="resourceTypeName">Node name</param>
        /// <returns>True if exists</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter resourcetype name is empty
        /// </exception>
        public bool ExistResourceType(string resourceTypeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceTypeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceTypeName"));
            }
            if (_managerclass.CacheInstances)
            {
                return _managerclass.ResourceTypeInstances.ContainsKey(resourceTypeName);
            }
            try
            {
                var res = new ClusterWmiHelpper().GetResourceType(resourceTypeName, _managerclass.ScopeManager);
                return (res != null);
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// get list of ResourceType
        /// </summary>
        /// <returns>Array of class ResourceType</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected ,
        /// Failed WMI method execution
        /// </exception>
        public ResourceTypeProperty[] GetResourceTypes()
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            try
            {
                return _managerclass.CacheInstances ? _managerclass.ResourceTypeInstances.Values.ToArray() : new ClusterWmiHelpper().GetResourceTypes(_managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.GetResourceTypes", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.GetResourceTypes", ex.Message), ex);
            }
        }
        /// <summary>
        /// Get a ResourceType
        /// </summary>
        /// <param name="resourcetypeName">name reourcetype</param>
        /// <returns>Resourcetype class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected,
        /// Parameter resourcetype name is empty, 
        /// Resourcetype not exist,
        /// Failed WMI method execution
        /// </exception>
        public ResourceTypeProperty GetResourceType(string resourcetypeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourcetypeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceTypeName"));
            }
            if (_managerclass.CacheInstances)
            {
                if (!_managerclass.ResourceTypeInstances.ContainsKey(resourcetypeName))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resourcetype", resourcetypeName));
                }
                return _managerclass.ResourceTypeInstances[resourcetypeName];
            }
            try
            {
                return new ClusterWmiHelpper().GetResourceType(resourcetypeName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.GetResourceType", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.GetResourceType", ex.Message), ex);
            }
        }

        ///<summary>
        /// Save a Resourcetype in Cluster
        ///</summary>
        ///<param name="resClass">Resourcetype Class to Save</param>
        /// <returns>
        /// ResourceTypeProperty Class
        /// </returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter resourcetype is null, 
        /// Resourcetype not exist, 
        /// Failed WMI method execution
        /// </exception>
        public ResourceTypeProperty Save(ResourceTypeProperty resClass)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (resClass == null)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "Resclass"));
            }
            if (_managerclass.CacheInstances)
            {
                if (!_managerclass.ResourceTypeInstances.ContainsKey(resClass.Name))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "ResourceType", resClass.Name));
                }
            }
            else
            {
                if (!ExistResourceType(resClass.Name))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "ResourceType", resClass.Name));
                }
            }
            try
            {
                resClass.SaveManagementObject(_managerclass.OperationSystem);
                var ret = new ClusterWmiHelpper().GetResourceType(resClass.Name, _managerclass.ScopeManager);
                if (_managerclass.CacheInstances)
                {
                    _managerclass.ResourceTypeInstances[resClass.Name] = ret;
                }
                return ret;
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.Save", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.Save", ex.Message), ex);
            }
        }

        ///<summary>
        /// Get a listresources of a particular type. 
        ///</summary>
        ///<param name="resourcetypeName">name resourcetype</param>
        ///<returns>array of resource class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter resourcetype name is empty, 
        /// Resourcetype not exist, 
        /// Failed WMI method execution
        /// </exception>
        public ResourceProperty[] GetResources(string resourcetypeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourcetypeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourcetypeName"));
            }
            if (!ExistResourceType(resourcetypeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resourcetype", resourcetypeName));
            }
            try
            {
                return new ClusterWmiHelpper().FindResourcesByResourceType(resourcetypeName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.GetResources", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "ResourceTypes.GetResources", ex.Message), ex);
            }
        }

        #endregion
    }
}
